﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ZombieMonkeys.Vibria
{
    public partial class SquareControl : UserControl
    {
        Logic.Square square;

        PuzzleGrid puzzleGrid;

        string[] noteValues = new string[4];
                

        /// <summary>
        /// Constructor for the square control
        /// </summary>
        /// <param name="i">row in the puzzle grid</param>
        /// <param name="j">column in the puzzle grid</param>
        /// <param name="s">reference to Logic.Square</param>
        public SquareControl(int r, int c, int g, Logic.Square s)
        {
            square = s;
            row = r;
            col = c;
            miniGrid = g;
            InitializeComponent();
            //Call the shading method
            SquareShading();
            //Draw the borders
            DrawBorder();
        }

        //Column number
        private int col;
        public int Col
        {
            get
            {
                return col;
            }
            set
            {
                col = value;
            }
        }

        //row number
        private int row;
        public int Row
        {
            get
            {
                return row;
            }
            set
            {
                row = value;
            }
        }

        //Minigrid number
        private int miniGrid;
        public int MiniGrid
        {
            get
            {
                return miniGrid;
            }
            set
            {
                miniGrid = value;
            }
        }

        /// <summary>
        /// reference to the instance of puzzle grid that created the square
        /// </summary>
        public PuzzleGrid GetPuzzleGrid
        {
            set
            {
                puzzleGrid = value;
            }
        }

        //text value to show in the square
        private string textValue;
        public string TextValue
        {
            get
            {
                return textValue;
            }
            set
            {
                if (value == "0")
                {
                    squareControlText.FontSize = 18;
                    textValue = noteValues[0] + " " + noteValues[1] + "\r" + noteValues[2] + " " + noteValues[3];
                    square.SetValue(0);
                }
                else
                {
                    squareControlText.FontSize = 32;
                    textValue = value;
                    square.SetValue(int.Parse(value));
                }
                squareControlText.Text = textValue;
            }
        }

        //highlight the square if an adjacent square is selected
        bool highLight = false;
        public bool HighLight
        {
            get
            {
                return highLight;
            }
            set
            {
                highLight = value;
                SquareShading();
            }
        }

        //allows the user to update a square or not
        bool permanent = false;
        public bool Permanent
        {
            get 
            {
                return permanent;
            }
            set
            {
                permanent = value;
                SquareShading();
            }
        }

        //the selected square can accept numbers from the button grid
        bool selected;
        public bool Selected
        {
            get
            {
                return selected;
            }
            set
            {
                selected = value;
                SquareShading();
            }
        }

        //Called after the hint button to update the GUI
        public void update()
        {
            if (square.GetValue() != "0")
            {
                squareControlText.FontSize = 32;
                squareControlText.Text = square.GetValue();
            }
            else
            {
                squareControlText.FontSize = 18;
                for (int i = 0; i < 4; i++)
                {
                    if (square.GetNoteValue(i) == "0")
                    {
                        noteValues[i] = string.Empty;
                    }
                    else
                    {
                        noteValues[i] = square.GetNoteValue(i);
                    }
                }
                squareControlText.Text = noteValues[0] + " " + noteValues[1] + "\r" + noteValues[2] + " " + noteValues[3];
            }
        }

        /// <summary>
        /// Square shading method to call when there is a change in the square's state
        /// </summary>
        private void SquareShading()
        {
            if (permanent)
	        {
		        squareControlText.Foreground = (SolidColorBrush)Resources["PhoneForegroundBrush"];
                LayoutRoot.Background = (SolidColorBrush)Resources["PhoneBackgroundBrush"];
	        }
            else if (selected)
            {
                squareControlText.Foreground = (SolidColorBrush)Resources["PhoneDisabledBrush"];
                LayoutRoot.Background = (SolidColorBrush)Resources["PhoneAccentBrush"];
            }
            else
            {
                squareControlText.Foreground = (SolidColorBrush)Resources["PhoneAccentBrush"];
                LayoutRoot.Background = (SolidColorBrush)Resources["PhoneBackgroundBrush"];
            }

            if (highLight)
            {
                LayoutRoot.Background = (SolidColorBrush)Resources["PhoneSemiTransparentBrush"];
            }
        }

        /// <summary>
        /// Draw the borders to look like a regular sudoku grid
        /// </summary>
        private void DrawBorder()
        {
            int top = 1;
            int right = 1;
            int left = 1;
            int bottom = 1;
            if (this.row % 3 == 0)
                top = 3;
            if (this.col % 3 == 0)
                left = 3;
            if (this.row == 8)
                bottom = 3;
            if (this.col == 8)
                right = 3;

            SquareControlBorder.BorderThickness = new Thickness(left, top, right, bottom);
        }

        /// <summary>
        /// Event handler for when the square is clicked
        /// </summary>
        public event EventHandler SquareClicked;

        /// <summary>
        /// Called when a square is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //first we clear any notegrids on the screen
            puzzleGrid.puzzleGridNoteList.Clear();
            if (puzzleGrid.puzzleGridNoteIndex.Count > 0)
            {
                for (int i = 0; i < puzzleGrid.puzzleGridNoteIndex.Count; i++)
                {
                    puzzleGrid.canvas1.Children.RemoveAt(puzzleGrid.puzzleGridNoteIndex[i]);
                    puzzleGrid.puzzleGridNoteIndex.Clear();
                }
            }
            //then we update each square
            puzzleGrid.Update();
            bool doubleClick = MouseButtonHelper.IsDoubleClick(sender, e);
            if (doubleClick)
            {
                OnDoubleClick(sender, e);
            }
            else
            { 
                OnClick(sender, e);
            }
        }

        private void OnClick(object sender, MouseButtonEventArgs e)
        {
            if (SquareClicked != null)
            {
                SquareClicked(this, null);
                if (!permanent)
                {
                    selected = true;
                    puzzleGrid.HighLightSquare();
                }
                SquareShading();
            }
        }

        private void OnDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (!permanent)
            {
                double fromLeft;
                double fromTop;
                NoteGrid noteGrid = new NoteGrid(square);
                noteGrid.LayoutRoot.Width = 80;
                noteGrid.LayoutRoot.Height = 80;
                puzzleGrid.canvas1.Children.Add(noteGrid);
                puzzleGrid.puzzleGridNoteIndex.Add(puzzleGrid.canvas1.Children.IndexOf(noteGrid));
                fromLeft = col * this.ActualWidth - (noteGrid.LayoutRoot.Width - this.ActualWidth) / 2;
                fromTop = row * this.ActualHeight - (noteGrid.LayoutRoot.Height - this.ActualHeight) / 2;
                Canvas.SetLeft(noteGrid, fromLeft);
                Canvas.SetTop(noteGrid, fromTop);
                puzzleGrid.puzzleGridNoteList.Add(noteGrid);
                this.selected = false;
            }
        }

    }
}
